package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

@Controller
@RequestMapping("/api")
public class LoginController {
    @Resource
    private TokenService tokenService;
    @Resource
    private UserService userService;
    /**
     * 登陆验证
     * @param name
     * @param password
     * @param request
     * @return
     */
        @RequestMapping(value = "/dologin",method = RequestMethod.POST,produces ="application/json")
        public @ResponseBody Dto login(@RequestParam String name,
        @RequestParam String password, HttpServletRequest request){
        try {
          ItripUser user=userService.login(name, MD5.getMd5(password,32));
            if(EmptyUtils.isNotEmpty(user)){
                String userAgent=request.getHeader("user-agent");
                String token=tokenService.generateToken(userAgent,user);
                tokenService.save(token,user);
                ItripTokenVO vo=new ItripTokenVO(token,Calendar.getInstance().getTimeInMillis()+2*60*60*1000,Calendar.getInstance().getTimeInMillis());
                return DtoUtil.returnDataSuccess(vo);
            }else
                return DtoUtil.returnFail("用户密码错误", ErrorCode.AUTH_AUTHENTICATION_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_AUTHENTICATION_FAILED);
        }
    }
    @RequestMapping(value = "/logout",method = RequestMethod.GET,produces = "application/json")
    public @ResponseBody Dto logout(HttpServletRequest request){
        String token=request.getHeader("token");
        try {
            if(tokenService.validate(request.getHeader("user-agent"),token)){
                tokenService.delete(token);
                return DtoUtil.returnSuccess();
            }
                else
                    return DtoUtil.returnFail("token无效",ErrorCode.AUTH_TOKEN_INVALID);
        } catch (Exception e) {
            return DtoUtil.returnFail("退出失败",ErrorCode.AUTH_TOKEN_INVALID);
        }

    }
    @RequestMapping(value = "/retoken",method = RequestMethod.POST,produces = "application/json")
    public @ResponseBody Dto reloadToken(HttpServletRequest request){
        String token= null;
        try {
            token = this.tokenService.reloadToken(request.getHeader("user-agent"),request.getHeader("token"));
            ItripTokenVO vo=new ItripTokenVO(token,
                    Calendar.getInstance().getTimeInMillis()+2*60*60*1000,
                    Calendar.getInstance().getTimeInMillis());
            return DtoUtil.returnDataSuccess(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getLocalizedMessage(),ErrorCode.AUTH_UNKNOWN);
        }

    }

}
